package com.ljfiction.book.activitys;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.MyAuhtorMessageBean;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import okhttp3.Call;
import okhttp3.Response;

public class MyAuthorMessageActivity extends BaseActivity {

    @BindView(R.id.author_message_recycle)
    RecyclerView authorMessageRecycle;
    @BindView(R.id.author_message_smartrefresh)
    SmartRefreshLayout authorMessageSmartrefresh;
    @BindView(R.id.nodata_author_message)
    LinearLayout nodataAuthorMessage;

    private BaseQuickAdapter adapter;
    private List<MyAuhtorMessageBean.DataBean.BookInfoMapBean> listData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_author_message);
        ButterKnife.bind(this);
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/getParagraphCommentMessageStatus")

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        authorMessageSmartrefresh.finishRefresh();
                        Gson gson = new Gson();
                        MyAuhtorMessageBean myAuhtorMessageBean = gson.fromJson(s, MyAuhtorMessageBean.class);
                        if (myAuhtorMessageBean.getReturnCode() == 200) {
                            nodataAuthorMessage.setVisibility(View.GONE);
                            listData.clear();
                            listData.addAll(myAuhtorMessageBean.getData().getBookInfoMap());
                            adapter.notifyDataSetChanged();
                        }else {
                            nodataAuthorMessage.setVisibility(View.VISIBLE);
                        }

                    }
                });
    }

    private void intView() {
        authorMessageSmartrefresh.setOnRefreshListener(refreshlayout -> intData());
        authorMessageRecycle.setLayoutManager(new GridLayoutManager(this, 2));
        authorMessageRecycle.setAdapter(adapter = new BaseQuickAdapter<MyAuhtorMessageBean.DataBean.BookInfoMapBean, BaseViewHolder>(R.layout.myauthormessage_item, listData) {
            @Override
            protected void convert(BaseViewHolder helper, MyAuhtorMessageBean.DataBean.BookInfoMapBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.myauthormessage_item_bookname, item.getBookName());
                Glide.with(mContext).load(item.getBookImage()).bitmapTransform(new RoundedCornersTransformation(mContext, 10, 1)).into((ImageView) helper.getView(R.id.myauthormessage_item_bookimage));
                if (item.getAllCounts() > 0) {
                    helper.getView(R.id.myauthormessage_item_pinglunnum).setVisibility(View.VISIBLE);
                    helper.setText(R.id.myauthormessage_item_num, "+" + String.valueOf(item.getAllCounts()));
                } else {
                    helper.getView(R.id.myauthormessage_item_pinglunnum).setVisibility(View.GONE);
                }
            }
        });
        adapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(MyAuthorMessageActivity.this, MyComplaintsActivity.class);
            intent.putExtra("bookId", String.valueOf(listData.get(position).getBookId()));
            intent.putExtra("CommCounts", listData.get(position).getCommCounts());
            intent.putExtra("ParCounts", listData.get(position).getParCounts());
            startActivity(intent);
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        intData();
    }

    @OnClick(R.id.author_message_back)
    public void onViewClicked() {
        goBack();
    }
}
